package com.hotel.controller.htcheckin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.controller.htcheckout.HtCheckOutController;
import com.hotel.dto.HtCheckInDto;
import com.hotel.dto.HtReservationDto;
import com.hotel.entity.*;
import com.hotel.service.htcheckin.HtCheckInService;
import com.hotel.service.htreservation.HtReservationService;
import com.hotel.service.htroom.HtRoomService;
import com.hotel.service.htroomtype.HtRoomTypeService;
import com.hotel.service.htstatus.HtStatusService;
import com.hotel.util.DateUtils;
import com.hotel.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/checkin")
@RestController
@CrossOrigin
public class HtCheckInController {

    @Resource
    private HtReservationService htReservationService;

    @Resource
    private HtRoomTypeService htRoomTypeService;

    @Resource
    private HtRoomService htRoomService;

    @Resource
    private HtCheckInService htCheckInService;

    private DateUtils dateUtils = new DateUtils();

    @PostMapping("/update")
    public R htCheckInup(HtReservation htReservation) {
        HtReservation htReservation2 = htReservationService.getById(htReservation);
        htReservation2.setStatusNo("3");
        htReservationService.updateById(htReservation2);

        HtCheckIn htCheckIn = new HtCheckIn();
        htCheckIn.setRoomNo(htReservation2.getRoomNo());
        htCheckIn.setRoomTypeNo(htReservation2.getRoomTypeNo());
        htCheckIn.setMembershipPrice(htReservation2.getMembershipPrice());
        htCheckIn.setDiscountedPrice(htReservation2.getDiscountedPrice());
        htCheckIn.setStandardPrice(htReservation2.getStandardPrice());
        htCheckIn.setCashPledge(htReservation2.getCashPledge());
        htCheckIn.setCheckInPerson(htReservation2.getPerson());
        htCheckIn.setIdNumber(htReservation2.getIdNumber());
        htCheckIn.setIdType(htReservation2.getIdType());
        htCheckIn.setPhone(htReservation2.getPhone());
        htCheckIn.setArrivalTime(htReservation2.getArrivalTime());
        htCheckIn.setDepartureTime(htReservation2.getDepartureTime());
        htCheckIn.setOccupancyNumber(htReservation2.getOccupancyNumber());
        htCheckIn.setMembershipPrice(htReservation2.getMembershipPrice());
        htCheckIn.setMemberNo(htReservation2.getMemberNo());
        htCheckIn.setServeBreakfast("未提供早餐");
        htCheckIn.setTimingWakeUp("未提供叫醒服务");
        htCheckIn.setCheckInRemark("未离开");

        htCheckInService.save(htCheckIn);

        return new R(R.OK);
    }

    @PostMapping("/list")
    public R htCheckInList(HtCheckInDto htCheckInDto) {
        try {
            //构造分页构造器对象
            Page<HtCheckIn> pageInfo = new Page<>(htCheckInDto.getPage(), htCheckInDto.getLimit());
            Page<HtCheckInDto> htCheckInDtoPage = new Page<>();

            LambdaQueryWrapper<HtCheckIn> htCheckInlqw = new LambdaQueryWrapper<>();
            htCheckInlqw.like(htCheckInDto.getCheckInNo() != null && htCheckInDto.getCheckInNo() != "", HtCheckIn::getCheckInNo, htCheckInDto.getCheckInNo());
            htCheckInlqw.like(htCheckInDto.getCheckInPerson() != null && htCheckInDto.getCheckInPerson() != "", HtCheckIn::getCheckInPerson, htCheckInDto.getCheckInPerson());
            htCheckInlqw.like(htCheckInDto.getRoomNo() != null && htCheckInDto.getRoomNo() != 0, HtCheckIn::getRoomNo, htCheckInDto.getRoomNo());
            if (htCheckInDto.getRoomTypeNo().equals("0")) {
                htCheckInDto.setRoomTypeNo(null);
            }
            htCheckInlqw.like(htCheckInDto.getRoomTypeNo() != null && htCheckInDto.getRoomTypeNo() != 0, HtCheckIn::getRoomTypeNo, htCheckInDto.getRoomTypeNo());
            htCheckInService.page(pageInfo, htCheckInlqw);

            //对象拷贝
            BeanUtils.copyProperties(pageInfo, htCheckInDtoPage);

            List<HtCheckIn> htCheckIns = pageInfo.getRecords();

            List<HtCheckInDto> list = htCheckIns.stream().map((item) -> {

                HtCheckInDto htCheckInDto1 = new HtCheckInDto();

                BeanUtils.copyProperties(item, htCheckInDto1);

                Long roomTypeNo = item.getRoomTypeNo();
                //根据id查询分类对象
                HtRoomType htRoomType = htRoomTypeService.getById(roomTypeNo);

                if (htRoomType != null) {
                    String htRoomTypeName = htRoomType.getRoomTypeName();
                    htCheckInDto1.setRoomTypeName(htRoomTypeName);
                }

                List<HtRoomType> htRooTypeList = htRoomTypeService.list();
                htRooTypeList.add(0, new HtRoomType((long) 0, "请选择"));
                htCheckInDto1.setHtRoomTypeList(htRooTypeList);
                return htCheckInDto1;
            }).collect(Collectors.toList());


            htCheckInDtoPage.setRecords(list);

            return new R(R.OK, htCheckInDtoPage);
        } catch (Exception e) {
            return new R(R.SERVER_ERROR);
        }
    }

    @PostMapping("/save")
    public R htCheckInSave(HtCheckIn htCheckIn) {
        if (htCheckIn.getCheckInRemark().equals("null")) {
            htCheckIn.setCheckInRemark("");
        }

        LambdaQueryWrapper<HtRoom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtRoom::getRoomNo, htCheckIn.getRoomNo());
        HtRoom one = htRoomService.getOne(lqw);
        if (htCheckIn.getCheckInNo() == "") {
            if (one != null && one.getStatusNo().equals("3")) {
                return new R(R.SERVER_ERROR, "该房间已入住");
            } else if (one != null && one.getStatusNo().equals("1")) {
                return new R(R.SERVER_ERROR, "该房间已预定");
            } else {
                one.setStatusNo("3");
                htRoomService.updateById(one);
                htCheckInService.save(htCheckIn);
            }
        } else {
            htCheckInService.updateById(htCheckIn);
        }
        return new R(R.OK);
    }

    @PostMapping("/updateNo")
    public R htCheckInUpNo(HtCheckIn htCheckIn) {

        HtCheckIn htCheckIn2 = htCheckInService.getById(htCheckIn);
        htCheckIn2.setCheckInRemark("已离开");
        htCheckIn2.setDepartureTime(dateUtils.strToDateLong(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));

        LambdaQueryWrapper<HtRoom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtRoom::getRoomNo, htCheckIn.getRoomNo());

        HtRoom htRoom = htRoomService.getOne(lqw);
        htRoom.setStatusNo("2");
        htRoomService.updateById(htRoom);
        htCheckInService.updateById(htCheckIn2);
        HtCheckOutController htCheckOutController = new HtCheckOutController();
        HtCheckOut htCheckOut = new HtCheckOut();
        htCheckOut.setCheckInNo(htCheckIn.getCheckInNo());
        htCheckOutController.htCheckOutSave(htCheckOut);
        return new R(R.OK);
    }

}
